//
//  SecondaryCatalogueController.m
//  KaneNote
//
//  Created by HuanJing on 2017/7/13.
//  Copyright © 2017年 HuanJing. All rights reserved.
//

//二级目录界面
#import "SecondaryCatalogueController.h"
#import "SecondaryClassModel.h"
#import "HrefJudgement.h"
#import "FoundationKnowledgeViewController.h"

@interface SecondaryCatalogueController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSString *srcStr;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

static NSString *const kSecondaryCatalogueCellID = @"SecondaryCatalogueCell";

@implementation SecondaryCatalogueController

- (instancetype)initWithSrc:(NSString *)srcString {
    if (self = [super init]) {
        _srcStr = srcString;
    }
    return self;
}

- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (UITableView *)tableView {
    if (_tableView == nil) {
        //CGRectMake(0, kNavBar_Height, kSCREEN_WIDTH, kSCREEN_HEIGHT - kNavBar_Height)
        _tableView = [[UITableView alloc] initWithFrame:kSCREEN style:UITableViewStylePlain];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self refreshTableViewData];
}

- (void)refreshTableViewData {

    @weakify(self)
    self.refreshDataBlock = ^{
        @strongify(self)
        [self.tableView reloadData];
    };
}

- (void)fetchData {
    NSString *fullSrcStr = [NSString stringWithFormat:@"%@/%@", HOST, self.srcStr];
    NSData *htmlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:fullSrcStr]];
    TFHpple *xPathParser = [[TFHpple alloc] initWithHTMLData:htmlData encoding:
                            HTML_ENCODING];
    NSArray *elements = nil;
    if ([self.srcStr isEqualToString:@"skills/index.htm"]) {
        elements = [xPathParser searchWithXPathQuery:@"//td[@height='35']//a"];
    } else {
        elements = [xPathParser searchWithXPathQuery:@"//td[@class='tiny']//a"];
    }
    for (NSInteger i = 0; i < elements.count; i++) {
        TFHppleElement *element = elements[i];
        SecondaryClassModel *model = [[SecondaryClassModel alloc] init];
        model.title = [element content];
        NSLog(@"%@", model.title);
        model.hrefStr = [element objectForKey:@"href"];
        [self.dataArray addObject:model];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kSecondaryCatalogueCellID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kSecondaryCatalogueCellID];
    }
    SecondaryClassModel *model = self.dataArray[indexPath.row];
    NSLog(@"%@", model.hrefStr);
    cell.textLabel.text = model.title;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SecondaryClassModel *model = self.dataArray[indexPath.row];
    Class cls = [HrefJudgement judgementHrefStr:model.hrefStr];
    UIViewController *vc = [[cls alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
